package com.mdlib.helper.ui.fragments.address;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.event.CityEvnet;
import com.mdlib.helper.model.AppModel;
import com.mdlib.helper.model.entity.CityEntity;
import com.mdlib.helper.ui.adapter.CityAdapter;
import com.mdlib.helper.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/12/1 10:03
 * 邮箱：hqy_xz@126.com
 */

public class CityFragment extends BaseTitleFragment {


    @Bind(R.id.et_city_name)
    EditText mEtCityName;
    @Bind(R.id.tv_city_name)
    TextView mTvCityName;
    @Bind(R.id.ll_city_name)
    LinearLayout mLlCityName;
    @Bind(R.id.rv_city_list)
    RecyclerView mRvCityList;

    private List<CityEntity> cityList = new ArrayList<>();

    private CityAdapter mCityAdapter;

    public static CityFragment newInstance() {
        return new CityFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_city;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.city_select_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        init();
    }

    public void init() {
        mTvCityName.setText(AppModel.getInstance().getAppLocationEntity().getCity());
        mCityAdapter = new CityAdapter(cityList);
        mRvCityList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvCityList.setAdapter(mCityAdapter);
        mRvCityList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                addFragment(AreaFragment.newInstance(cityList.get(position).getId()));
            }
        });
        getCity();
    }

    @OnClick(R.id.ll_city_name)
    public void onClick() {
        EventBus.getDefault().post(new CityEvnet(mTvCityName.getText().toString()));
        getActivity().finish();

    }

    private void getCity() {
        addSubscribe(DataManager.getInstance().getUserApi()
                .getCity()
                .compose(RxHelper.<ArrayList<CityEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<CityEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<CityEntity> cityEntities) {
                        for (CityEntity city : cityEntities) {
                            cityList.add(city);
                        }
                        mCityAdapter.notifyDataSetChanged();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));
    }


}
